﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace GameBase
{
    public class Perspective3DGroup : VisibleGroup
    {
        protected Camara camara;
        float viewDistance;
        Viewport viewport;

        public Viewport Viewport
        {
            get { return viewport; }
            set { viewport = value; }
        }
        public override float Scale
        {
            get { return scale; }
            set
            {
                scale = value;
                UpdateCamara();
                foreach (Visble3DObject child in listChildren)
                {
                    child.MaxDepth = viewDistance / GetDrawScale();
                    child.BaseScale = scale * baseScale;
                }
            }
        }
       
        public float ViewDistance
        {
            get { return viewDistance; }
            set { 
                viewDistance = value;
                UpdateCamara();
                foreach (Visble3DObject child in listChildren)
                {
                    child.MaxDepth = viewDistance / GetDrawScale();
                    child.BaseScale = scale * baseScale;
                }
            }
        }
        public override void AddChild(VisibleGameEntity child)
        {
            try
            {
                Visble3DObject obj3d = (Visble3DObject)child;
                obj3d.MaxDepth = viewDistance / GetDrawScale();
                obj3d.Parent = this;
                ListChildren.Add(obj3d);
            }
            catch
            {
            }
        }
        public Camara Camara
        {
            get { return camara; }
            set { camara = value; }
        }
        public Perspective3DGroup(Viewport viewport, float viewDistance)
        {
            camara = new Camara();
            camara.SetView(new Vector3(0, 0, viewDistance),
                new Vector3(0, 0, 0),
                new Vector3(0, 1, 0));
            camara.SetProjection(MathHelper.PiOver2,
                (float)viewport.Width/viewport.Height,
            0.1f, viewDistance);
            ViewDistance = viewDistance;
            this.viewport = viewport;
        }
        public override Vector2 Position
        {
            get { return position; }
            set
            {
                position = value;
                UpdateCamara();
            }
        }
        public override Vector2 BasePosition
        {
            get
            {
                return base.BasePosition;
            }
            set
            {
                basePosition = value;
                UpdateCamara();
                foreach (Visble3DObject child in listChildren)
                {
                    child.MaxDepth = viewDistance / GetDrawScale();
                    child.BaseScale = scale * baseScale;
                }
            }
        }
        public override float BaseScale
        {
            get
            {
                return base.BaseScale;
            }
            set
            {
                baseScale = value;
                UpdateCamara();
            }
        }
        private void UpdateCamara()
        {
            camara.SetView(new Vector3(GetDrawPosition().X, GetDrawPosition().Y, viewDistance / GetDrawScale()),
            new Vector3(GetDrawPosition().X, GetDrawPosition().Y, 0),
            new Vector3(0, 1, 0));

        }
        public override Vector2 GetDrawPosition()
        {
            //return base.GetDrawPosition();
            return new Vector2(position.X - basePosition.X , position.Y + basePosition.Y );
        }
        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            /*foreach (Visble3DObject child in listChildren)
            {
                Vector3 pos = child.GetDrawPosition(camara, Viewport);
                //pos = camara.Project(pos,viewport);
                child.Draw(gameTime, spriteBatch, pos);
            }*/
            for (int i= 0;i<listChildren.Count;i++)
            {
                Visble3DObject child = (Visble3DObject)listChildren[i];
                Vector3 pos = child.GetDrawPosition(camara, Viewport);
                //pos = camara.Project(pos,viewport);
                child.Draw(gameTime, spriteBatch, pos);
            }
        }
        /*
        public override void Translate(float x, float y)
        {
            base.Translate(-x, y);
        }*/
        /*public Visble3DObject GetObjectAtPosition(float x, float y)
        {
            foreach (Visble3DObject obj in listChildren)
            {
                Vector3 pos = obj.GetDrawPosition(camara, viewport);
                //float 
                Rectangle bound = obj.Sprite.GetBound(
                    new Vector2(pos.X,pos.Y),GetDrawScale());
                if (IsHover(x, y, bound))
                {
                    return obj;
                }
                //bound.
            }
            return null;
        }*/
    }
}
